#ifndef SINGLETON_HPP
#define SINGLETON_HPP

template <class T>
class Singleton
{
public:

    // the singleton must be constructed with a reference to the controlled object
    //----------------------------------------------------------------------
    Singleton(T& object)
    {
        Q_ASSERT_X(!_instance, "constructor", "Only one instance of this class is permitted.");
        _instance = &object;
    }

    // the singleton accessor
    //----------------------------------------------------------------------
    ~Singleton()
    {
        Q_ASSERT_X(_instance, "destructor", "The singleton instance is invalid.");
        _instance = 0;
    }

    // the singleton accessor
    //----------------------------------------------------------------------
    static T& instance()
    {
        Q_ASSERT_X(_instance, "instancing", "The singleton has not yet been created.");
        return (*_instance);
    }

private:
    // Data...
    //----------------------------------------------------------------------
    static T* _instance;

    // Nonexistant Functions...
    //----------------------------------------------------------------------
    Singleton(const Singleton& src);
    Singleton& operator=(const Singleton& src);

};
template <class T> T* Singleton<T>::_instance = 0; /*!< TODO */

#endif // SINGLETON_HPP
